/* global process,__dirname:true*/

const path = require("path");
const webpack = require("webpack");

// const EX = require("extract-text-webpack-plugin"); //css 单独打包
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //css 单独打包
// const EXCSS = new EX('[name].css');

const env=process.env.NODE_ENV;
console.log("env",env);
const port="8081";
const entry={
    vendor:["react","react-dom","react-router","react-router-dom","react-redux",
        "redux-thunk","redux","lodash","natty-fetch-fd","nprogress","leaflet"],
    index:"./src/app/app.js"
};
const mode= env?'production':'development';

const plugins= [
    new MiniCssExtractPlugin({
        filename: mode === 'development' ? '[name].css' : '[name].[hash].css',
        chunkFilename: mode === 'development' ? '[id].css' : '[id].[hash].css',
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.ProvidePlugin({
        React:"react"
    }),
    new webpack.DefinePlugin({
        '__ENV__':JSON.stringify(env)
    })
];


module.exports ={
    mode:mode,
    entry:entry,
    output: {
        filename: "[name].js",
        path: path.resolve(__dirname,"dist")
    },
    performance:{
        hints:false
    },
    optimization:{
        splitChunks:{
            cacheGroups:{
                vendor:{
                    name:'vendor',
                    chunks:'initial',
                    minChunks:2
                }
            }
        }
    },
    devServer: {
        disableHostCheck: true,
        // historyApiFallback:true,
        hot: true,
        inline: true, // 实时刷新
        host: '0.0.0.0',
        port,
        /*devServe跨域代理配置*/
        proxy:{
            '/proxy':{
                target:"http://localhost:8083",
                pathRewrite:{'^/proxy':''},
                changeOrigin:true,
                secure:false
            }

        }
    },
    resolve: {
        alias: {
            '@':path.resolve(__dirname,'src')
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                include: [
                    path.resolve(__dirname,'src')
                ],
                loader: 'babel-loader'
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit:8192
                        }
                    }
                ]
            },
            {
                test: /\.(less|css)$/,
                exclude: /node_modules/,
                use: [
                    mode === 'development' ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'css-loader',
                    'less-loader'
                ],
            }

        /*    ,{
                test: /\.(less|css)$/,
                exclude: /node_modules/,
                use: EXCSS.extract({
                    fallback:'style-loader',
                    use:['css-loader','less-loader']
                })
            }*/
            ,{
                test: /\.css$/,
                exclude: /src/,
                use: [
                    {
                        loader: "style-loader"

                    },{
                        loader: 'css-loader',
                        options: {
                            importLoaders:1
                        }
                    }
                ]
            },{
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: "eslint-loader"
            }
        ],
    },
    plugins
};
